package com.chushouya.product.rabbitmq.sender;

import com.chushouya.common.constant.RabbitMqQueueEnum;
import com.chushouya.common.util.RabbitMQUtil;
import com.general.framework.core.exception.Ex;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 速回收同步消息发送器
 * 用于发送速回收品牌和产品同步消息
 * 
 * @author system
 */
@Component
@Slf4j
public class SuhuishouSyncMessageSender {

    @Resource
    private RabbitMQUtil rabbitMQUtil;

    /**
     * 发送品牌同步消息（立即发送）
     * 
     * @param categoryId 分类ID
     */
    public void sendBrandSyncMessage(Long categoryId) {
        try {
            validateCategoryId(categoryId);
            
            log.info("发送速回收品牌同步消息，分类ID: {}", categoryId);
            rabbitMQUtil.sendMessage(RabbitMqQueueEnum.SUHUISHOU_BRAND_SYNC.getQueueName(), categoryId);
            log.info("速回收品牌同步消息发送成功，分类ID: {}", categoryId);
        } catch (Exception e) {
            log.error("发送速回收品牌同步消息异常，分类ID: {}, 错误信息: {}", categoryId, e.getMessage(), e);
            throw new RuntimeException("发送速回收品牌同步消息失败", e);
        }
    }

    /**
     * 发送品牌同步消息（延时发送）
     * 
     * @param categoryId 分类ID
     * @param delaySeconds 延时秒数
     */
    public void sendBrandSyncMessage(Long categoryId, int delaySeconds) {
        try {
            validateCategoryId(categoryId);
            
            log.info("发送速回收品牌同步延时消息，分类ID: {}, 延时: {}秒", categoryId, delaySeconds);
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.SUHUISHOU_BRAND_SYNC.getQueueName(), 
                categoryId, 
                delaySeconds, 
                TimeUnit.SECONDS
            );
            log.info("速回收品牌同步延时消息发送成功，分类ID: {}, 延时: {}秒", categoryId, delaySeconds);
        } catch (Exception e) {
            log.error("发送速回收品牌同步延时消息异常，分类ID: {}, 延时: {}秒, 错误信息: {}", 
                    categoryId, delaySeconds, e.getMessage(), e);
            throw new RuntimeException("发送速回收品牌同步延时消息失败", e);
        }
    }

    /**
     * 发送产品同步消息（立即发送）
     * 
     * @param categoryId 分类ID
     * @param brandId 品牌ID
     */
    public void sendProductSyncMessage(Long categoryId, Long brandId) {
        try {
            validateCategoryId(categoryId);
            validateBrandId(brandId);
            
            log.info("发送速回收产品同步消息，分类ID: {}, 品牌ID: {}", categoryId, brandId);
            
            // 创建消息内容
            SuhuishouProductSyncMessage message = new SuhuishouProductSyncMessage();
            message.setCategoryId(categoryId);
            message.setBrandId(brandId);
            
            rabbitMQUtil.sendMessage(RabbitMqQueueEnum.SUHUISHOU_PRODUCT_SYNC.getQueueName(), message);
            log.info("速回收产品同步消息发送成功，分类ID: {}, 品牌ID: {}", categoryId, brandId);
        } catch (Exception e) {
            log.error("发送速回收产品同步消息异常，分类ID: {}, 品牌ID: {}, 错误信息: {}", 
                    categoryId, brandId, e.getMessage(), e);
            throw new RuntimeException("发送速回收产品同步消息失败", e);
        }
    }

    /**
     * 发送产品同步消息（延时发送）
     * 
     * @param categoryId 分类ID
     * @param brandId 品牌ID
     * @param delaySeconds 延时秒数
     */
    public void sendProductSyncMessage(Long categoryId, Long brandId, int delaySeconds) {
        try {
            validateCategoryId(categoryId);
            validateBrandId(brandId);
            
            log.info("发送速回收产品同步延时消息，分类ID: {}, 品牌ID: {}, 延时: {}秒", 
                    categoryId, brandId, delaySeconds);
            
            // 创建消息内容
            SuhuishouProductSyncMessage message = new SuhuishouProductSyncMessage();
            message.setCategoryId(categoryId);
            message.setBrandId(brandId);
            
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.SUHUISHOU_PRODUCT_SYNC.getQueueName(), 
                message, 
                delaySeconds, 
                TimeUnit.SECONDS
            );
            log.info("速回收产品同步延时消息发送成功，分类ID: {}, 品牌ID: {}, 延时: {}秒", 
                    categoryId, brandId, delaySeconds);
        } catch (Exception e) {
            log.error("发送速回收产品同步延时消息异常，分类ID: {}, 品牌ID: {}, 延时: {}秒, 错误信息: {}", 
                    categoryId, brandId, delaySeconds, e.getMessage(), e);
            throw new RuntimeException("发送速回收产品同步延时消息失败", e);
        }
    }

    /**
     * 批量发送品牌同步消息
     * 
     * @param categoryIds 分类ID列表
     */
    public void sendBrandSyncMessageBatch(Long[] categoryIds) {
        if (categoryIds == null || categoryIds.length == 0) {
            log.warn("分类ID列表为空，跳过批量发送品牌同步消息");
            return;
        }
        
        log.info("开始批量发送速回收品牌同步消息，分类数量: {}", categoryIds.length);
        
        for (int i = 0; i < categoryIds.length; i++) {
            try {
                // 每个消息间隔一定时间发送，避免API调用过于频繁
                int delaySeconds = i * 2; // 每2秒发送一个
                sendBrandSyncMessage(categoryIds[i], delaySeconds);
            } catch (Exception e) {
                log.error("批量发送品牌同步消息失败，分类ID: {}", categoryIds[i], e);
            }
        }
        
        log.info("批量发送速回收品牌同步消息完成，分类数量: {}", categoryIds.length);
    }

    /**
     * 验证分类ID
     */
    private void validateCategoryId(Long categoryId) {
        if (categoryId == null || categoryId <= 0) {
            throw Ex.business("分类ID无效");
        }
    }

    /**
     * 验证品牌ID
     */
    private void validateBrandId(Long brandId) {
        if (brandId == null || brandId <= 0) {
            throw Ex.business("品牌ID无效");
        }
    }

    /**
     * 速回收产品同步消息
     */
    public static class SuhuishouProductSyncMessage {
        private Long categoryId;
        private Long brandId;

        public Long getCategoryId() {
            return categoryId;
        }

        public void setCategoryId(Long categoryId) {
            this.categoryId = categoryId;
        }

        public Long getBrandId() {
            return brandId;
        }

        public void setBrandId(Long brandId) {
            this.brandId = brandId;
        }

        @Override
        public String toString() {
            return "SuhuishouProductSyncMessage{" +
                    "categoryId=" + categoryId +
                    ", brandId=" + brandId +
                    '}';
        }
    }
}
